#!/bin/sh
# TheBugGenie post-receive hook for git, for direct access (git and tbg on same machine)
# To use, this needs to be marked executable, and linked in to your git repo,
# user@server:/path/to/repo/.git/hooks $ 
#   ln -s /tbg_install/modules/vcs_integration/hooks/git/tbg-post-receive post-receive
#   chmod +x /tbg_install/modules/vcs_integration/hooks/git/tbg-post-receive

#>>>>> User config

# projectid comes from http://tbg_server/configure/module/vcs_integration
# on the project settings tab
projectid=1

# this is the path to the installed thebuggenie.
tbg_cli_path=/home/...

#<<<<< End of user config

update_tbg()
{
    oldhash=$1
    newhash=$2
    refname=$3

    # Not working? uncomment the echos and see what's not arriving properly

    #echo "Attempting to update tbg with oldhash:$oldhash newhash:$newhash"
    changedfiles=`git diff-tree --name-status -r $newhash --no-commit-id`
    name=`git log ^$oldhash $newhash --pretty=format:"%an <%ae>"`
    log=`git log ^$oldhash $newhash --pretty=format:"%s %b"`
    time=`git log ^$oldhash $newhash --pretty=format:"%ct"`

    #echo "updating with name: $name"
    #echo "updating with log: $log"
    #echo "updating with time: $time"
    #echo "updating files: $changedfiles"

    cd $tbg_cli_path
    ./tbg_cli vcs_integration:report_commit $projectid "$name" $newhash "$log" "$changedfiles" $oldhash $time
}

if [ -n "$1" -a -n "$2" ]; then
    # we seem to be in command line mode...
    update_tbg $1 $2
else
    while read oldhash newhash refname
    do
        # we seem to be operating as a git post-receive hook
        update_tbg $oldhash $newhash $refname
    done
fi


